public class eventeditoverride{
    public eventeditoverride(ApexPages.StandardController controller) {
        this.controller = controller;
    }    
  public pagereference newedit(){
    
    id eid = ApexPages.currentPage().getParameters().get('id'); 
    List<event> eventl=new List<event>();
    eventl=[select whatid from event where id=:eid];
    if(eventl[0].whatid!=null){
        eid=eventl[0].whatid;
        PageReference ref=new PageReference('/'+eid+'/e?retURL=%2F'+eid);
        ref.setRedirect(true);
        return ref;
        }
        PageReference ref=new PageReference('/'+eid+'/e?retURL=%2F'+eid+'&nooverride=1');
        ref.setRedirect(true);
        return ref;
  }
 private final ApexPages.StandardController controller;
 
    static testMethod void eventeditoverrideTest()
    {
        Profile p  = [select id from profile where name = 'System Administrator'or name =: '系统管理员'];
        
        User u1 = new User (alias = 'standt', email = 'shuaicao@isoftstone.com', emailencodingkey='UTF-8', 
                lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', Title__c = 'Sales Rep',profileid = p.id, 
                timezonesidkey = 'America/Los_Angeles', username='standarduser@testorg1.com');
        insert u1;

        NationalAttendee__c nat = new NationalAttendee__c();
        HCPEducation__c hcpe = new HCPEducation__c();
        hcpe.date__c = date.Today() + 10;
        List<recordtype>  saloonIdl = [select id from recordtype where sobjecttype=:'HCPEducation__c' and name=:'学术沙龙'];
        recordtype saloonId = saloonIdl.get(0);
        hcpe.RecordTypeId = saloonId.Id;
        hcpe.place__c = 'place';
        insert hcpe; 
    
        event add = new event();
        add.OwnerId  = u1.Id;
        add.whatid = hcpe.id;
        add.Subject = '拜访';
        add.ActivityDateTime = dateTime.Now();
        add.DurationInMinutes = 50;
        
        insert add;
              
        test.setCurrentPageReference(Page.eventbuttonoverride);
        System.currentPageReference().getParameters().put('id', add.Id); 
        
        ApexPages.StandardController del = new ApexPages.StandardController(add);
        eventeditoverride testCase = new eventeditoverride(del);
        testCase.newedit(); 
    } 
   
}

//e?retURL=%2F  *this is edit button's url,and add the id before it is use for when you chick the cancel button the page will return to the record which you modified;